@extends('admin.layout.index')

@section('content')
@if (count($errors) > 0)
    <div class="mws-form-message warning">
      验证失败
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

<div class="mws-panel grid_8">
  <div class="mws-panel-header">
    <span>{{$title}}</span></div>
  <div class="mws-panel-body no-padding">
    <form class="mws-form" action="/admin/config/{{$data['conf_id']}}" id="conf_form" method="post" enctype="mulipart/form-data">

    {{csrf_field()}}
    {{method_field('put')}}
      <div class="mws-form-inline">
        <div class="mws-form-row">
          <label class="mws-form-label">标　题：</label>
          <div class="mws-form-item">
            <input type="text" class="small" name="conf_title" value="{{$data['conf_title']}}"></div>
        </div>
        <div class="mws-form-row">
          <label class="mws-form-label">名　称：</label>
          <div class="mws-form-item">
            <input type="text" class="small" name="conf_name" value="{{$data['conf_name']}}"></div>
        </div>
        <div class="mws-form-row">
          <label class="mws-form-label">类　型：</label>
          <div id="in_check" class="mws-form-item" style="width:420px">
            <input type="radio" {{$data['field_type'] == 'input' ? 'checked':''}} name="field_type" value="input" checked onclick="showTr(this)">input　
            <input type="radio" {{$data['field_type'] == 'textarea' ? 'checked':''}} name="field_type" value="textarea" onclick="showTr(this)">textarea　
            <input type="radio" {{$data['field_type'] == 'radio' ? 'checked':''}} name="field_type" value="radio" onclick="showTr(this)">radio
            <input type="radio" {{$data['field_type'] == 'img' ? 'checked':''}} name="field_type" value="img" onclick="showTr(this)">img
          </div>
        </div>
        <div class="mws-form-row" id="field_value" style="display:none;">
          <label class="mws-form-label">类型值：</label>
          <div class="mws-form-item">

            <input type="text" class="lg" name="field_value" value="{{$data['field_value'] or ''}}">
            <p><i class="fa fa-exclamation-circle yellow"></i>类型值只有在radio的情况下才需要配置，格式 1|开启,0|关闭</p>
          </div>
        </div>

        <div class="mws-form-row" id="ifield_value" style="display:none;">
          <label class="mws-form-label">图片上传：</label>
          <div class="mws-form-item">
            <input type="hidden" id="conf_content" name="conf_content" value="{{$data['conf_content'] or ''}}">
            <input type="file" id="file_upload"  name="field" value="">
            <img src="" alt="">
          </div>
        </div>
        <div class="mws-form-row">
          <label class="mws-form-label">说　明：</label>
          <div class="mws-form-item">
            <textarea name="conf_tips" cols="50" rows="5">{{$data['conf_tips']}}</textarea>
          </div>
        </div>
        
      </div>
    
      <div class="mws-button-row">
        <input type="submit" value="提交" class="btn btn-danger">
        <input type="reset" value="重置" class="btn "></div>
    </form>
  </div>
</div>
<script type="text/javascript">
        $(function () {
            $("#file_upload").change(function () {
                uploadImage();
            });
        });

        function uploadImage() {

//                            判断是否有选择上传文件
            var imgPath = $("#file_upload").val();
            // alert(imgPath);
            if (imgPath == "") {
                alert("请选择上传图片！");
                return;
            }
            //判断上传文件的后缀名
            var strExtension = imgPath.substr(imgPath.lastIndexOf('.') + 1);
            if (strExtension != 'jpg' && strExtension != 'gif'
                && strExtension != 'png' && strExtension != 'bmp') {
                alert("请选择图片文件");
                return;
            }
//                            var myform = document.getElementById('art_from');
            var formData = new FormData();
            formData.append("field", $('#file_upload')[0].files[0]);
            formData.append("_token", '{{csrf_token()}}');
            console.log(formData);
            $.ajax({
                type: "POST",
                url: "/admin/config/upload",
                data: formData,
                async: true,
                cache: false,
                contentType: false,
                processData: false,
                success: function(data) {
//                                    console.log(data);
//                                    alert("上传成功");
//                    $('#img1').attr('src','http://ovf9o4wws.bkt.clouddn.com/'+data);
//                    http://project187.oss-cn-shanghai.aliyuncs.com/uploads/201708290957357759.jpg
                   // $('#img1').attr('src','http://project187.oss-cn-shanghai.aliyuncs.com/'+data);
                    $('#conf_content').val(data);

                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert("上传失败，请检查网络后重试");
                }
            });
        }

    </script>

 <script>
    var dat = $('input:checked').val();
     switch (dat)
       {
          case 'radio':
            $('#field_value').show();
            break;
           case 'img':
            $('#ifield_value').show();
            break;
       }

    function showTr(obj){
       var ele_type =  $(obj).val();
       // if(ele_type == 'radio'){
       //     $('#field_value').show();
       // }else{
       //     $('#field_value').hide();
       // }
       //  if(ele_type == 'radio'){
       //     $('#field_value').show();
       // }else if(ele_type == 'img'){
       //     $('#ifield_value').show();
       // }else{
       //    $('#field_value').hide();
       //    $('#ifield_value').hide();
       // }
       switch (ele_type)
       {
          case 'radio':
            $('#field_value').show();
            $('#ifield_value').hide();
            break;
           case 'img':
            $('#ifield_value').show();
            $('#field_value').hide();
            break;
          default :
            $('#field_value').hide();
            $('#ifield_value').hide();
       }
    }

</script>
@endsection